﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace PM.PMBusiness
{
    /// <summary>
    /// approveschedual 的摘要说明
    /// </summary>
    public class approveschedual
    {
        public approveschedual()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 获取一个计划的实体类
        /// </summary>
        /// <param name="plan">计划编号</param>
        /// <returns>返回计划实体类</returns>
        public PM.Model.ProjectPlan getPlanData(string plan)
        {
            PM.Model.ProjectPlan oneplan = new PM.Model.ProjectPlan();
            string sqlstr = "select * from View_ProjectPlan where plancode='" + plan + "'";
            SqlDataReader myreadr = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr);
            while(myreadr.Read())
            {
                oneplan.plancode = myreadr[1].ToString();
                oneplan.typename = myreadr[12].ToString();
                oneplan.plevel = myreadr[3].ToString();
                oneplan.ptype = myreadr[4].ToString();
                oneplan.opendate = DateTime.Parse(myreadr[5].ToString());
                oneplan.closedate = DateTime.Parse(myreadr[6].ToString());
                oneplan.builtdate = DateTime.Parse(myreadr[7].ToString());
                oneplan.remark = myreadr[11].ToString() == string.Empty ? myreadr[11].ToString() : "无备注";
            }
            myreadr.Close();
            return oneplan;
        }
        /// <summary>
        /// 增加一项审批计划
        /// </summary>
        /// <param name="approve">审批计划实体类</param>
        /// <returns>返回执行结果</returns>
        public int Add(PM.Model.ApprovePlan approve)
        {
                SqlParameter[] parameters = {
					new SqlParameter("@approvecode", SqlDbType.NVarChar,50),
					new SqlParameter("@plancode", SqlDbType.NVarChar,50),
					new SqlParameter("@status", SqlDbType.SmallInt,2),
					new SqlParameter("@opentime", SqlDbType.DateTime),
					new SqlParameter("@closetime", SqlDbType.DateTime),
					new SqlParameter("@operate", SqlDbType.NVarChar,50)};
                parameters[0].Value = approve.approvecode;
                parameters[1].Value = approve.plancode;
                parameters[2].Value = approve.status;
                parameters[3].Value = approve.opentime;
                parameters[4].Value = approve.closetime;
                parameters[5].Value = approve.operate;

                return PM.Code.DAL.DbHelper.RunProcedure1("Pdmis_ApprovePlanAdd", parameters);
        }

        /// <summary>
        /// 创建div信息框
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>返回源</returns>
        public string getProjectPlan(string date)
        {
            //新开启项目:项目开始时间>当前时间，即项目还没有开启
            //过期项目:项目的结束时间<当前时间
            //正在执行的项目:项目开始时间<当前时间<项目结束时间
            StringBuilder sqlstr = new StringBuilder("select * from (");
            StringBuilder divHtml = new StringBuilder();
            sqlstr.AppendFormat("select top 3 *,1 as tag from Bus_ApprovePlan where opentime>'{0}'", date);
            sqlstr.Append(" and status=1 order by opentime) as a union select * from (");
            sqlstr.AppendFormat("select top 3 *,3 as tag from Bus_ApprovePlan where opentime<='{0}' and closetime>='{0}'", date);
            sqlstr.Append(" and status=1 order by opentime desc) as b union select * from (");
            sqlstr.AppendFormat("select top 3 *,2 as tag from Bus_ApprovePlan where closetime<'{0}'", date);
            sqlstr.Append(" and status=1 order by closetime desc) as c order by tag");

            SqlDataReader result = PM.Code.DAL.DbHelper.ExecuteDataReaderBySql(sqlstr.ToString());
            if (result.HasRows)
            {
                //绘制html
                createDiv(ref divHtml, result);
            }
            divHtml.Append("?无任何数据?无任何数据?无任何数据");
            return divHtml.ToString();
        }
        /// <summary>
        /// 创建div
        /// </summary>
        /// <param name="divHtml">引用源</param>
        /// <param name="aresult">sqldatareader结果</param>
        private void createDiv(ref StringBuilder divHtml, SqlDataReader aresult)
        {
            int tag = 1;//标记
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
            while (aresult.Read())
            {
                if (aresult[8].ToString().Trim() != tag.ToString())
                {
                    tag = Int16.Parse(aresult[8].ToString().Trim());
                    if (aresult[8].ToString().Trim() == "2"&&count1==0)
                    {
                        divHtml.Append("<h2>无任何数据</h2>");
                    }
                    if (aresult[8].ToString().Trim() == "3")
                    {
                        if (count1 == 0 && count2 == 0)
                            divHtml.Append("<h2>无任何数据</h2>?<h2>无任何数据</h2>");
                        if (count1 != 0 && count2 == 0)
                            divHtml.Append("?<h2>无任何数据</h2>");
                    }
                    divHtml.Append("?");
                }
                if (aresult[8].ToString().Trim() == "1")
                {
                    divHtml.AppendFormat("<h2>{0}于{1}开启</h2>", aresult[1].ToString(), aresult[3].ToString());
                    count1++;
                }
                else if (aresult[8].ToString().Trim() == "2")
                {
                    divHtml.AppendFormat("<h2>{0}于{1}关闭</h2>", aresult[1].ToString(), aresult[4].ToString());
                    count2++;
                }
                else
                {
                    divHtml.AppendFormat("<h2>{0}正在执行中……</h2>", aresult[1].ToString());
                    count3++;
                }
            }
            aresult.Close();
        }
        /// <summary>
        /// 更新一个审批计划
        /// </summary>
        /// <param name="id">表的唯一标识</param>
        /// <param name="opentime">开启时间</param>
        /// <param name="closetime">结束时间</param>
        public int Update_ApprovePlan(int tid,string opentime,string closetime)
        {
            try
            {
                SqlParameter[] PlanParameter ={ 
                new SqlParameter("@id",SqlDbType.Int),
                new SqlParameter("@opendate",SqlDbType.DateTime),
                new SqlParameter("@closedate",SqlDbType.DateTime)};
                PlanParameter[0].Value = tid;
                PlanParameter[1].Value = opentime;
                PlanParameter[2].Value = closetime;
                StringBuilder sqlstr = new StringBuilder("update Bus_ApprovePlan set opentime=@opendate,closetime=@closedate where id=@id");
                return PM.Code.DAL.DbHelper.ExecuteSql(sqlstr.ToString(), PlanParameter);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除一项审批计划
        /// </summary>
        /// <param name="appcode">审批计划编号</param>
        /// <param name="pcode">审批对象编号</param>
        /// <returns>返回执行结果</returns>
        public int Delete_ApprovePlan(string appcode,string pcode)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@approvecode", SqlDbType.NVarChar,50),
					new SqlParameter("@plancode", SqlDbType.NVarChar,50)};
            parameters[0].Value = appcode;
            parameters[1].Value = pcode;

            return PM.Code.DAL.DbHelper.RunProcedure1("Pdmis_ApprovePlanDel", parameters);
        }
    }
}
